<template>
  <el-dialog
    :title="!dataForm.liveId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="userid">
      <el-input v-model="dataForm.userid" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="用户名称" prop="username">
      <el-input v-model="dataForm.username" placeholder="用户名称"></el-input>
    </el-form-item>
    <el-form-item label="商品id" prop="goodsid">
      <el-input v-model="dataForm.goodsid" placeholder="商品id"></el-input>
    </el-form-item>
    <el-form-item label="直播url" prop="urls">
      <el-input v-model="dataForm.urls" placeholder="直播url"></el-input>
    </el-form-item>
    <el-form-item label="直播标题" prop="liveTittle">
      <el-input v-model="dataForm.liveTittle" placeholder="直播标题"></el-input>
    </el-form-item>
    <el-form-item label="直播图片" prop="liveImg">
      <el-input v-model="dataForm.liveImg" placeholder="直播图片"></el-input>
    </el-form-item>
    <el-form-item label="直播状态" prop="liveStatus">
      <el-input v-model="dataForm.liveStatus" placeholder="直播状态"></el-input>
    </el-form-item>
    <el-form-item label="直播唯一标识" prop="liveWeiyi">
      <el-input v-model="dataForm.liveWeiyi" placeholder="直播唯一标识"></el-input>
    </el-form-item>
    <el-form-item label="直播人数" prop="countpelop">
      <el-input v-model="dataForm.countpelop" placeholder="直播人数"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createtime">
      <el-input v-model="dataForm.createtime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="结束时间" prop="enttime">
      <el-input v-model="dataForm.enttime" placeholder="结束时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          liveId: 0,
          userid: '',
          username: '',
          goodsid: '',
          urls: '',
          liveTittle: '',
          liveImg: '',
          liveStatus: '',
          liveWeiyi: '',
          countpelop: '',
          createtime: '',
          enttime: ''
        },
        dataRule: {
          userid: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          username: [
            { required: true, message: '用户名称不能为空', trigger: 'blur' }
          ],
          goodsid: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ],
          urls: [
            { required: true, message: '直播url不能为空', trigger: 'blur' }
          ],
          liveTittle: [
            { required: true, message: '直播标题不能为空', trigger: 'blur' }
          ],
          liveImg: [
            { required: true, message: '直播图片不能为空', trigger: 'blur' }
          ],
          liveStatus: [
            { required: true, message: '直播状态不能为空', trigger: 'blur' }
          ],
          liveWeiyi: [
            { required: true, message: '直播唯一标识不能为空', trigger: 'blur' }
          ],
          countpelop: [
            { required: true, message: '直播人数不能为空', trigger: 'blur' }
          ],
          createtime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          enttime: [
            { required: true, message: '结束时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.liveId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.liveId) {
            this.$http({
              url: this.$http.adornUrl(`/live/liveacticity/info/${this.dataForm.liveId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userid = data.liveActicity.userid
                this.dataForm.username = data.liveActicity.username
                this.dataForm.goodsid = data.liveActicity.goodsid
                this.dataForm.urls = data.liveActicity.urls
                this.dataForm.liveTittle = data.liveActicity.liveTittle
                this.dataForm.liveImg = data.liveActicity.liveImg
                this.dataForm.liveStatus = data.liveActicity.liveStatus
                this.dataForm.liveWeiyi = data.liveActicity.liveWeiyi
                this.dataForm.countpelop = data.liveActicity.countpelop
                this.dataForm.createtime = data.liveActicity.createtime
                this.dataForm.enttime = data.liveActicity.enttime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/live/liveacticity/${!this.dataForm.liveId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'liveId': this.dataForm.liveId || undefined,
                'userid': this.dataForm.userid,
                'username': this.dataForm.username,
                'goodsid': this.dataForm.goodsid,
                'urls': this.dataForm.urls,
                'liveTittle': this.dataForm.liveTittle,
                'liveImg': this.dataForm.liveImg,
                'liveStatus': this.dataForm.liveStatus,
                'liveWeiyi': this.dataForm.liveWeiyi,
                'countpelop': this.dataForm.countpelop,
                'createtime': this.dataForm.createtime,
                'enttime': this.dataForm.enttime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
